package com.douxg.learning.common;

import java.text.SimpleDateFormat;

/**
 * @ClassName FwWorkTest
 * @Description 阜外医院相关
 * @Author douxg
 * @Date 2020/1/19 11:57
 * @Version 1.0
 */
public class FwWorkTest2 {
    public static void main(String[] args) {
//        System.out.println(isInRange("192.168.1.127", "192.168.1.64/26"));
//        System.out.println(isInRange("192.168.1.2", "192.168.0.0/23"));
//        System.out.println(isInRange("192.168.0.1", "192.168.0.0/24"));
//        System.out.println(isInRange("192.168.0.1", "192.168.0.0/32"));
//        System.out.println(isInRange("192.168.1.1", "192.168.1.0/31"));
        System.out.println(isInRange("10.19.10.130", "10.19.10.128/25"));

//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
//        String startTime = sdf.format(System.currentTimeMillis() - (15 * 60 * 1000) - (8 * 60 * 60 * 1000));
//        System.out.println(startTime);
    }

    public static boolean isInRange(String ip, String cidr) {
        String[] ips = ip.split("\\.");
        int ipAddr = (Integer.parseInt(ips[0]) << 24)
                | (Integer.parseInt(ips[1]) << 16)
                | (Integer.parseInt(ips[2]) << 8) | Integer.parseInt(ips[3]);
//        int type = Integer.parseInt(cidr.replaceAll(".*/", ""));
        String[] split = cidr.split("/");
        int type = Integer.parseInt(split[1]);
        int mask = 0xFFFFFFFF << (32 - type);
        String cidrIp = split[0];
        String[] cidrIps = cidrIp.split("\\.");
        int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24)
                | (Integer.parseInt(cidrIps[1]) << 16)
                | (Integer.parseInt(cidrIps[2]) << 8)
                | Integer.parseInt(cidrIps[3]);
        return (ipAddr & mask) == (cidrIpAddr & mask);
    }

    public static int ipRangeNetCode(String ipRange) {
        String[] split = ipRange.split("/");
        int type = Integer.parseInt(split[1]);
        int mask = 0xFFFFFFFF << (32 - type);
        String cidrIp = split[0];
        String[] cidrIps = cidrIp.split("\\.");
        int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24)
                | (Integer.parseInt(cidrIps[1]) << 16)
                | (Integer.parseInt(cidrIps[2]) << 8)
                | Integer.parseInt(cidrIps[3]);
        return cidrIpAddr & mask;
    }

    public static int ipNetCode(String ip, int mask) {
        String[] ips = ip.split("\\.");
        int ipAddr = (Integer.parseInt(ips[0]) << 24)
                | (Integer.parseInt(ips[1]) << 16)
                | (Integer.parseInt(ips[2]) << 8) | Integer.parseInt(ips[3]);
        return ipAddr & mask;
    }
}
